<?php

namespace services;
class BaiduocrService
{
    //测试
    private $API_KEY;
    private $SECRET_KEY;



    private function init($appid=null,$apikey=null,$secretkey=null)
    {
        $this->appid = !empty($appid) ? $appid : config('baiduocr.APPID');
        $this->apikey = !empty($apikey) ? $apikey : config('baiduocr.API_KEY');
        $this->secretkey = !empty($secretkey) ? $secretkey : config('baiduocr.SECRET_KEY');
    }

    public function __construct($appid=null,$apikey=null,$secretkey=null)
    {
        $this->init($appid,$apikey,$secretkey);
        $this->__loader();
    }

    private function __loader()
    {
        require_once(dirname(dirname(__FILE__)) . '/baiduzhineng/' . 'AipOcr.php');
        require_once(dirname(dirname(__FILE__)) . '/baiduzhineng/' . 'lib/AipBase.php');
    }

    //ocr
    public function ocr($url){
        $AipOcr = new \AipOcr($this->appid,$this->apikey,$this->secretkey);
        $options=[];
        $options['language_type'] = 'auto_detect';
        $res = $AipOcr->basicAccurateUrl($url,$options);
        if (isset($res['error_code']) && $res['error_code']) {
            \think\Log::write('百度ocr错误'.$url.json_encode($res,JSON_UNESCAPED_UNICODE));
            return false;
        }
        $words_res = array_column($res['words_result'], 'words');
        $words=['content' => implode('', $words_res),'words_result_num'=>$res['words_result_num'],'log_id'=>$res['log_id']];

        return $words;
    }


}